/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package PassLogic;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Hashtable;
import java.util.TreeSet;

/**
 *
 * @author harab
 */
public class PassLogic extends javax.swing.JApplet {
    
    private static final Hashtable<Byte, String> allowedSymbols = new Hashtable<Byte, String>(256);
    private static final char[] allSymbols = {'a', '<', 'b', ',', 'c', '.', 'd', '>', 'e', '?', 'f', '/', 'g', ';', 'h', ':', 'i',
    '|', 'j', '}', 'k', ']', 'l', '[', 'm', '{', 'n', '=', 'o', '+', 'p', '-', 'q', '_', 'r', ')', 's', '(', 't', '*', 'u', '&', 'v',
    '^', 'w', '%', 'x', '$', 'y', '#', 'z', '@', 'A', '!', 'B', '~', 'C','`', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
    'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
    private static final TreeSet<String> bannedSymbols = new TreeSet<String>();
    private static int outputlen = 12;
    
    private static void initializeDict() {
    	bannedSymbols.add("\"");
    	bannedSymbols.add("\\");
    	bannedSymbols.add("\'");
        bannedSymbols.add("`");
        bannedSymbols.add("~");
        bannedSymbols.add("#");
        bannedSymbols.add("%");
        bannedSymbols.add("^");
        bannedSymbols.add("(");
        bannedSymbols.add(")");
        bannedSymbols.add("-");
        //bannedSymbols.add("+");
        bannedSymbols.add("+");
        bannedSymbols.add("{");
        bannedSymbols.add("[");
        bannedSymbols.add("}");
        bannedSymbols.add("]");
        bannedSymbols.add("|");
        bannedSymbols.add(";");
        bannedSymbols.add(":");
        bannedSymbols.add("/");
        bannedSymbols.add(".");
        bannedSymbols.add(">");
        bannedSymbols.add(",");
        bannedSymbols.add("<");
        bannedSymbols.add("=");
        int j = 0;
    	for(int i = 0; i < 256; i++) {
            if(!bannedSymbols.contains(String.valueOf(allSymbols[j % allSymbols.length]))) {
                allowedSymbols.put((byte)i, String.valueOf(allSymbols[j % allSymbols.length]));
            } else {
                while(bannedSymbols.contains(String.valueOf(allSymbols[++j % allSymbols.length]))) {
                        //j++;
                }
                allowedSymbols.put((byte)i, String.valueOf(allSymbols[j % allSymbols.length]));
            }
            j++;
    	}
    }

    /**
     * Initializes the applet PassLogic
     */
    @Override
    public void init() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PassLogic.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PassLogic.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PassLogic.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PassLogic.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the applet */
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * This method is called from within the init() method to initialize the
     * form. WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        jMenuBar3 = new javax.swing.JMenuBar();
        jMenu5 = new javax.swing.JMenu();
        jMenu6 = new javax.swing.JMenu();
        jFrame1 = new javax.swing.JFrame();
        logic1TXTF = new javax.swing.JTextField();
        logic2TXTF = new javax.swing.JTextField();
        logic3TXTF = new javax.swing.JTextField();
        logic4TXTF = new javax.swing.JTextField();
        logic5TXTF = new javax.swing.JTextField();
        cypherBTN = new javax.swing.JButton();
        jTextField6 = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        jMenu3.setText("File");
        jMenuBar2.add(jMenu3);

        jMenu4.setText("Edit");
        jMenuBar2.add(jMenu4);

        jMenu5.setText("File");
        jMenuBar3.add(jMenu5);

        jMenu6.setText("Edit");
        jMenuBar3.add(jMenu6);

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setPreferredSize(new java.awt.Dimension(335, 350));

        logic1TXTF.setText("Logic1");
        logic1TXTF.setName(""); // NOI18N
        logic1TXTF.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                logic1TXTFFocusGained(evt);
            }
        });

        logic2TXTF.setText("Logic2");
        logic2TXTF.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                logic2TXTFFocusGained(evt);
            }
        });

        logic3TXTF.setText("Logic3");
        logic3TXTF.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                logic3TXTFFocusGained(evt);
            }
        });

        logic4TXTF.setText("Logic4");
        logic4TXTF.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                logic4TXTFFocusGained(evt);
            }
        });

        logic5TXTF.setText("Logic5");
        logic5TXTF.setToolTipText("");
        logic5TXTF.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                logic5TXTFFocusGained(evt);
            }
        });

        cypherBTN.setText("Cypher");
        cypherBTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cypherBTNActionPerformed(evt);
            }
        });

        jTextField6.setEditable(false);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(logic1TXTF, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                            .addComponent(logic2TXTF)
                            .addComponent(logic3TXTF)
                            .addComponent(logic4TXTF)
                            .addComponent(logic5TXTF))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(cypherBTN))
                    .addComponent(jTextField6))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logic1TXTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logic2TXTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logic3TXTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logic4TXTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logic5TXTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 105, Short.MAX_VALUE)
                .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cypherBTN)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cypherBTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cypherBTNActionPerformed
       initializeDict();
       MessageDigest md;
        try {
            md = MessageDigest.getInstance("SHA-512");
            byte[][] hash = new byte[5][];
            hash[0] = md.digest(logic1TXTF.getText().getBytes());
            hash[1] = md.digest(logic2TXTF.getText().getBytes());
            hash[2] = md.digest(logic3TXTF.getText().getBytes());
            hash[3] = md.digest(logic4TXTF.getText().getBytes());
            hash[4] = md.digest(logic5TXTF.getText().getBytes());
            StringBuilder sb = new StringBuilder(hash[0].length);
            //sb.append(dict.get((byte)(hash[0][0] ^ hash[1][0])));
            for(int i = 0; i < hash[0].length && i < outputlen*(hash[0].length/outputlen); i+=(hash[0].length/(outputlen))) {
                int b = 0;
                for (int j = 0; j < 5; j++) {
                    b ^= hash[j][i];
                }
                sb.append(allowedSymbols.get((byte)b));
            }
            jTextField6.setText(sb.toString());
            //editText2.setText(Integer.toHexString(hash[0]));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_cypherBTNActionPerformed

    private void logic1TXTFFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_logic1TXTFFocusGained
        logic1TXTF.selectAll();
    }//GEN-LAST:event_logic1TXTFFocusGained

    private void logic2TXTFFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_logic2TXTFFocusGained
        logic2TXTF.selectAll();
    }//GEN-LAST:event_logic2TXTFFocusGained

    private void logic3TXTFFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_logic3TXTFFocusGained
        logic3TXTF.selectAll();
    }//GEN-LAST:event_logic3TXTFFocusGained

    private void logic4TXTFFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_logic4TXTFFocusGained
        logic4TXTF.selectAll();
    }//GEN-LAST:event_logic4TXTFFocusGained

    private void logic5TXTFFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_logic5TXTFFocusGained
        logic5TXTF.selectAll();
    }//GEN-LAST:event_logic5TXTFFocusGained


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cypherBTN;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuBar jMenuBar3;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField logic1TXTF;
    private javax.swing.JTextField logic2TXTF;
    private javax.swing.JTextField logic3TXTF;
    private javax.swing.JTextField logic4TXTF;
    private javax.swing.JTextField logic5TXTF;
    // End of variables declaration//GEN-END:variables
}
